//
//  Demo.swift
//  learnSwitf
//
//  Created by Open on 3/25/15.
//  Copyright (c) 2015 open-groupe. All rights reserved.
//

import Cocoa

extension NSViewController
{
    private struct Associatedkeys {
        static var DescripticeName = "nsh_DescriptionName"
    }
    
    var descriptiveName: String? {
        get {
            return objc_getAssociatedObject(self, &Associatedkeys.DescripticeName) as? String
        }
        set {
            if let newValue = newValue {
                objc_setAssociatedObject(self, &Associatedkeys.DescripticeName, newValue as NSString?, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
            }
        }
    }
}

extension NSViewController
{
    public  override class func initialize() {
        struct Static {
            static var token: dispatch_once_t = 0
        }
        
        if self !== NSViewController.self {
            return
        }
        
        dispatch_once(&Static.token) {
            let originalSelector = Selector ("viewWillAppeal")
            let swizzledSelector = Selector("ns_viewWillAppear")
            
            let originalMethod = class_getInstanceMethod(self, originalSelector)
            let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
            
            let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
            if didAddMethod {
                class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
            }else
            {
                method_exchangeImplementations(originalMethod, swizzledMethod)
            }
        }
    }
    
    func ns_viewWillAppear(animated: Bool) {
        self.ns_viewWillAppear(animated)
        
    }
}
